﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;
using StampController.BL;
using StampController.BL.Models;

namespace StampController.Pages
{
    public partial class PendingConfirmList : BasePage
    {
        private const int PageSize = 20;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Get list of the request form completed by sites
        /// </summary>
        /// <param name="status"></param>
        /// <param name="pageNum"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [WebMethod]
        public static object GetList(string status, int pageNum, string keyword)
        {
            var user = SessionUtils.Get<UsersModel>("USER");

            if (user != null)
            {
                int count;
                var im = new RequestFormImpl();
                var sites = GetSites(user.UserID, user.SiteID);
                IList<RequestFormModel> list = im.GetRequestFormList(sites, status, keyword, null, null, pageNum, PageSize,
                    out count);

                var obj = new GridTable<RequestFormModel>
                {
                    PageNum = pageNum,
                    RowCount = count,
                    PageSize = PageSize,
                    List = list,
                    Approval = false
                };

                return obj;
            }
            return null;
        }

        /// <summary>
        /// Delegate
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="siteId"></param>
        /// <returns></returns>
        private static List<int> GetSites(string userId, int siteId)
        {
            var imAuthorized = new AuthorizedHistoryImpl();
            var sites = new List<int> { siteId };
            var users = imAuthorized.GetAuthorizedPersonList(userId, DateTime.Now);
            sites.AddRange(users.Where(x => x.IsAdminOfficer).Select(x => x.SiteID));
            // Only get the admin
            return sites;
        }
    }
}